package com.app.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.app.R;
import com.app.constant.Constants;
import com.app.http.HttpDataLoader;
import com.app.http.HttpDataReqUrl;
import com.app.model.request.InviteReq;
import com.app.model.response.ResObj;
import com.app.utils.ToastUtil;
import com.app.view.ClearWriteEditText;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ActivityInvitecode extends ActivityBaseMenuText {

    private static final String LOG_TAG = ActivityInvitecode.class.getSimpleName();
    @BindView(R.id.etContent)
    ClearWriteEditText etContent;

    private HttpDataLoader dataLoader;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invitecode);
        ButterKnife.bind(this);
        setTitle("输入邀请码");
        dataLoader = new HttpDataLoader(this);
        setMenuText("完成");
        setOnMenuClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (etContent.getText() == null || etContent.getText().length() < 1) {
                    ToastUtil.getInstance().showToast(ActivityInvitecode.this, "邀请码不能为空，请重新输入");
                } else {
                    checkInvitecode();
                }
            }
        });
    }

    private void checkInvitecode() {
        InviteReq req = new InviteReq();
        req.setInviteCode(etContent.getText().toString());
        dataLoader.POST(HttpDataReqUrl.URL_CHECK_INVITE_CODE, req);
    }

    @Override
    public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
        Log.e(LOG_TAG, "onRequestSuccess");
        Log.e(LOG_TAG, "url: " + url);
        Log.e(LOG_TAG, "result: " + result);
        if (url.equals(HttpDataReqUrl.URL_CHECK_INVITE_CODE)) {
            if (result.getCode() == Constants.CODE_SUCCESS) {
                ToastUtil.getInstance().showToast(ActivityInvitecode.this, result.getMsg());
                finish();
            } else {
                ToastUtil.getInstance().showToast(ActivityInvitecode.this, result.getMsg());
            }
        }


    }

    @Override
    public void onRequestError(String url, int statusCode, String error) {
        Log.e(LOG_TAG, "onRequestError");
        Log.e(LOG_TAG, "url: " + url);
        Log.e(LOG_TAG, "statusCode: " + statusCode);
        Log.e(LOG_TAG, "error: " + error);
        ToastUtil.getInstance().showToast(ActivityInvitecode.this, error);
    }
}
